﻿using G19Glower.Shared;
using System;

namespace G19Glower.Glowies.DiscoGlower
{
    internal class DiscoGlower
    {
        private IGlowController mController;
        private IKeyboardListener mKeyboardListener;
        
        private bool mRunning = false;
        
        internal DiscoGlower(IGlowController controller, IKeyboardListener keyboardListener)
        {
            mController = controller;
            mKeyboardListener = keyboardListener;
        }

        public bool IsRunning { get { return mRunning; } }

        public void Start()
        {
            if (mRunning)
            {
                throw new InvalidOperationException();
            }

            mRunning = true;
            mKeyboardListener.KeyPressed += this.KeyboardHooker_KeyPressed;
        }

        public void Stop()
        {
            if (!mRunning)
            {
                throw new InvalidOperationException();
            }

            mRunning = false;
            mKeyboardListener.KeyPressed -= this.KeyboardHooker_KeyPressed;
        }

        private void KeyboardHooker_KeyPressed(System.Windows.Forms.Keys obj)
        {
            var values = new byte[3];
            new Random().NextBytes(values);
            mController.SetColor(values[0], values[1], values[2]);
        }
    }
}
